package com.dz.lm.service.favorite.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dz.lm.common.dto.FavoriteDTO;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.service.AbstractService;
import com.dz.lm.service.favorite.FavoriteService;


@Service
public class FavoriteServiceImpl  extends AbstractService implements FavoriteService{
	
	@Resource(name="readFavoriteDao")
	private com.dz.lm.dao.favorite.read.FavoriteDao readFavoriteDao;
	
	@Resource(name="writeFavoriteDao")
	private com.dz.lm.dao.favorite.write.FavoriteDao writeFavoriteDao;

	@Override
	public List<FavoriteDTO> ListFavorite() throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FavoriteDTO selectByPrimaryKey(Integer favoriteId) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> insert(Map<String, Object> qryParam) throws ServiceException {
		Map<String, Object> retData = new HashMap<String, Object>();
		try {
			int count = readFavoriteDao.qryFavoriteByUserIdAndProductIdCount(qryParam);
			if (count > 0) {
				count = writeFavoriteDao.delete(qryParam);
				if (count > 0) {
					retData.put("result", "00");
					retData.put("message", "取消成功");
				} else {
					retData.put("result", "01");
					retData.put("message", "取消失败");
				}
			} else {
				count = writeFavoriteDao.insert(qryParam);
				if (count > 0) {
					retData.put("result", "00");
					retData.put("message", "收藏成功");
				} else {
					retData.put("result", "01");
					retData.put("message", "收藏失败");
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
		return retData;
	}

	@Override
	public int delete(Map<String, Object> qryParam) throws ServiceException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> qryProductFavoriteList(Map<String, Object> param) throws ServiceException {
		Map<String, Object> retMap = new HashMap<String, Object>();
		List<Map<String, Object>> favoriteList = null;
		Integer count = 0;
		try {
			// 返回数据明细
			favoriteList = readFavoriteDao.qryFavoriteListByUserId(param);
			// 返回数据总条数
			count = readFavoriteDao.qryFavoriteListByUserIdCount(param);
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
		retMap.put("favoriteList", favoriteList);
		retMap.put("count", count);
		return retMap;
	}
}
